************************************
* Using the whole data and iv
************************************
*1. Global First Stage
clear

do "$do/new_imm_data.do" /*Preparing on new immigrants*/

do "$do/final_data_full.do"

***************************
* Table 2: First Stage
***************************
use "$temp/regdata_finaltables_full.dta", clear


forvalues i=1(1)2{
estimates clear
qui {
estimates clear
*Area
areg skratio_lit zratio_smith_lit  yrdum*  areadumcent`i'_* dcentury`i' [aw=weight] if  (capsamp|Horsesamp) & xzsamp, absorb(area) cluster(area)
predict skratio_noind`i'
estimates store noind

*Industry
areg skratio_lit zratio_smith_lit areadum_*   yrdum* areadumcent`i'_* dcentury`i' [aw=weight] if  (capsamp|Horsesamp) & xzsamp, absorb(industry_unified) cluster(area)
predict skratio_area`i'
estimates store area 

*Industry*Year
areg skratio_lit zratio_smith_lit areadum_* inddum_*  yrdum* areadumcent`i'_* dcentury`i' [aw=weight] if (capsamp|Horsesamp) &  xzsamp , absorb(indyear) cluster(area)
predict skratio_indyr`i'
estimates store indyr
}
}

gen skratio_area=skratio_area2
gen skratio_noind=skratio_noind2
gen skratio_indyr=skratio_indyr2


foreach x in skratio_area skratio_noind skratio_indyr skratio_noind1 skratio_area1 skratio_indyr1  skratio_noind2 skratio_area2 skratio_indyr2 {
gen `x'_dearly1=`x'*dearly1
gen `x'_dlate1=`x'*dlate1
gen `x'_dearly2=`x'*dearly2
gen `x'_dlate2=`x'*dlate2
}



save "$temp/regdata_finaltables_xz_full.dta", replace
 



 
use "$temp/regdata_finaltables_xz_full.dta", clear
 * Keep kl
drop lnkyimpute lnFuy lnHpYimpute
rename lnklimpute lnkimpute
rename lnFul lnFuel
rename lnHplimpute lnHpimpute
gen dumkl=1
save "$temp/regdata_kl.dta", replace

*Keep ky
use "$temp/regdata_finaltables_xz_full.dta", clear
drop lnklimpute lnFul lnHplimpute
rename lnkyimpute lnkimpute
rename lnFuy lnFuel
rename lnHpYimpute lnHpimpute
gen dumkl=0
save "$temp/regdata_ky.dta", replace


* Append Data
use "$temp/regdata_ky.dta", clear
append using "$temp/regdata_kl.dta"
save "$temp/regdata_k", replace




use "$temp/regdata_k", clear
gen dumky=(dumkl==0)

forvalues i=1(1)2{
foreach x in skratio_dearly`i' skratio_dlate`i'  skratio_area`i'_dearly`i' skratio_area`i'_dlate`i'{
gen `x'_dumkl=`x'*dumkl
gen `x'_dumky=`x'*dumky
}
}
forvalues i=1(1)2{
foreach x in skratio_noind`i'_dearly`i' skratio_noind`i'_dlate`i'  skratio_indyr`i'_dearly`i' skratio_indyr`i'_dlate`i'{
gen `x'_dumkl=`x'*dumkl
gen `x'_dumky=`x'*dumky
}
}





forvalues i=1(1)180 {
gen dumkl_areadumcent_`i'=areadumcent2_`i'*dumkl
}

forvalues i=1(1)180 {
gen dumkl_areadum_`i'=areadum_`i'*dumkl
}
forvalues i=1(1)146 {
gen dumkl_inddum_`i'=inddum_`i'*dumkl
}
forvalues i=1(1)9 {
gen dumkl_yrdum`i'=yrdum`i'*dumkl
}
forvalues i=1(1)1018 {
gen dumkl_itdum_`i'=itdum_`i'*dumkl
}


gen dumkl_dcentury1=dumkl*dcentury1
gen dumkl_dcentury2=dumkl*dcentury2


forvalues i=1(1)180 {
gen dumkl_areadumcent1_`i'=areadumcent1_`i'*dumkl
gen dumkl_areadumcent2_`i'=areadumcent2_`i'*dumkl
}


global dumkl_indyr "dumkl_*"
global dumkl_area  " dumkl_areadumcent_*  dumkl_areadum_* dumkl_yrdum* dumkl_dcentury dumkl_inddum_*" 
global dumkl_noind " dumkl_areadumcent_*  dumkl_areadum_* dumkl_yrdum* dumkl_dcentury" 


forvalues i=1(1)180 {
gen dumky_areadumcent_`i'=areadumcent2_`i'*dumky
gen dumky_areadum_`i'=areadum_`i'*dumky
}
forvalues i=1(1)146 {
gen dumky_inddum_`i'=inddum_`i'*dumky
}
forvalues i=1(1)9 {
gen dumky_yrdum`i'=yrdum`i'*dumky
}

forvalues i=1(1)1018 {
gen dumky_itdum_`i'=itdum_`i'*dumky
}


gen dumky_dcentury1=dumky*dcentury1
gen dumky_dcentury2=dumky*dcentury2


* Define new dependent variables to estimate -A	
foreach var in kimpute Fuel Hpimpute{
forvalues i=1(1)2{
*Industry effects
gen ln`var'_`i'=ln`var'-(1-.8599459)*skratio_dearly`i' /*k/l*/
replace ln`var'_`i'=ln`var'-(1-.8599459)*skratio_dlate`i' if dlate`i' & dumkl /*k/l*/
replace ln`var'_`i'=ln`var'-.0787483*skratio_dearly`i' if dearly`i' & dumky /*k/y*/
replace ln`var'_`i'=ln`var'-.0787483*skratio_dlate`i' if dlate`i' & dumky /*k/y*/

}
}

* 2. Define new dependent variables to estimate (1-A)
foreach var in kimpute Fuel Hpimpute{
forvalues i=1(1)2{
*Industry effects
gen ln`var'_hat_`i'=ln`var'+.8599459*skratio_dearly`i' /*k/l*/
replace ln`var'_hat_`i'=ln`var'+.8599459*skratio_dlate`i' if dlate`i' & dumkl /*k/l*/
replace ln`var'_hat_`i'=ln`var'+.5084719*skratio_dearly`i' if dearly`i' & dumky /*k/y*/
replace ln`var'_hat_`i'=ln`var'+.5084719*skratio_dlate`i' if dlate`i' & dumky /*k/y*/

}
}



forvalues i=1(1)2{
* Endogenous X
gen skratiohat_dearly`i'=skratio_dearly`i' /*k/n*/
replace skratiohat_dearly`i'= (.0787483+.5084719)*skratio_dearly`i' if  dumky /*k/y*/
gen skratiohat_dlate`i'=skratio_dlate`i' /*k/n*/
replace skratiohat_dlate`i'= (.0787483+.5084719)*skratio_dlate`i' if  dumky /*k/y*/


/*Instrument*/
* Area effects
gen skratiohatz_noind_dearly`i'=skratio_noind_dearly`i' /*k/l*/
replace skratiohatz_noind_dearly`i'= (.0787483+.5084719)*skratio_noind_dearly`i' if  dumky /*k/y*/
gen skratiohatz_noind_dlate`i'=skratio_noind_dlate`i' /*k/l*/
replace skratiohatz_noind_dlate`i'= (.0787483+.5084719)*skratio_noind_dlate`i' if  dumky /*k/y*/

*Industry Effects
gen skratiohatz_area_dearly`i'=skratio_area_dearly`i' /*k/n*/
replace skratiohatz_area_dearly`i'= (.0787483+.5084719)*skratio_area_dearly`i' if  dumky /*k/y*/
gen skratiohatz_area_dlate`i'=skratio_area_dlate`i' /*k/n*/
replace skratiohatz_area_dlate`i'= (.0787483+.5084719)*skratio_area_dlate`i' if  dumky /*k/y*/

*IndustryxYear Effect
gen skratiohatz_indyr_dearly`i'=skratio_indyr_dearly`i' /*k/l*/
replace skratiohatz_indyr_dearly`i'= (.0787483+.5084719)*skratio_indyr_dearly`i' if  dumky /*k/y*/
gen skratiohatz_indyr_dlate`i'=skratio_indyr_dlate`i' /*k/l*/
replace skratiohatz_indyr_dlate`i'= (.0787483+.5084719)*skratio_indyr_dlate`i' if  dumky /*k/y*/
}



/*Instrument by century*/
forvalues i=1(1)2{
* Area effects
gen skratiohatz_noind`i'_dearly`i'=skratio_noind`i'_dearly`i' /*k/l*/
replace skratiohatz_noind`i'_dearly`i'= (.0787483+.5084719)*skratio_noind`i'_dearly`i' if  dumky /*k/y*/
gen skratiohatz_noind`i'_dlate`i'=skratio_noind`i'_dlate`i' /*k/l*/
replace skratiohatz_noind`i'_dlate`i'= (.0787483+.5084719)*skratio_noind`i'_dlate`i' if  dumky /*k/y*/

*Industry Effects
gen skratiohatz_area`i'_dearly`i'=skratio_area`i'_dearly`i' /*k/n*/
replace skratiohatz_area`i'_dearly`i'= (.0787483+.5084719)*skratio_area`i'_dearly`i' if  dumky /*k/y*/
gen skratiohatz_area`i'_dlate`i'=skratio_area`i'_dlate`i' /*k/n*/
replace skratiohatz_area`i'_dlate`i'= (.0787483+.5084719)*skratio_area`i'_dlate`i' if  dumky /*k/y*/

*IndustryxYear Effect
gen skratiohatz_indyr`i'_dearly`i'=skratio_indyr`i'_dearly`i' /*k/l*/
replace skratiohatz_indyr`i'_dearly`i'= (.0787483+.5084719)*skratio_indyr`i'_dearly`i' if  dumky /*k/y*/
gen skratiohatz_indyr`i'_dlate`i'=skratio_indyr`i'_dlate`i' /*k/l*/
replace skratiohatz_indyr`i'_dlate`i'= (.0787483+.5084719)*skratio_indyr`i'_dlate`i' if  dumky /*k/y*/
}




forvalues i=1(1)2{
* Endogenous var.
gen skratiohat_dearly`i'_dumkl=skratiohat_dearly`i'*dumkl
gen skratiohat_dlate`i'_dumkl=skratiohat_dlate`i'*dumkl

gen skratiohat_dearly`i'_dumky=skratiohat_dearly`i'*dumky
gen skratiohat_dlate`i'_dumky=skratiohat_dlate`i'*dumky


** Instrument defined for three groups of FE
gen skratiohatz_area_dearly`i'_dumkl=skratiohatz_area_dearly`i'*dumkl
gen skratiohatz_area_dlate`i'_dumkl=skratiohatz_area_dlate`i'*dumkl
gen skratiohatz_noind_dearly`i'_dumkl=skratiohatz_noind_dearly`i'*dumkl
gen skratiohatz_noind_dlate`i'_dumkl=skratiohatz_noind_dlate`i'*dumkl
gen skratiohatz_indyr_dearly`i'_dumkl=skratiohatz_indyr_dearly`i'*dumkl
gen skratiohatz_indyr_dlate`i'_dumkl=skratiohatz_indyr_dlate`i'*dumkl

gen skratiohatz_area_dearly`i'_dumky=skratiohatz_area_dearly`i'*dumky
gen skratiohatz_area_dlate`i'_dumky=skratiohatz_area_dlate`i'*dumky
gen skratiohatz_noind_dearly`i'_dumky=skratiohatz_noind_dearly`i'*dumky
gen skratiohatz_noind_dlate`i'_dumky=skratiohatz_noind_dlate`i'*dumky
gen skratiohatz_indyr_dearly`i'_dumky=skratiohatz_indyr_dearly`i'*dumky
gen skratiohatz_indyr_dlate`i'_dumky=skratiohatz_indyr_dlate`i'*dumky

** Instrument defined for three groups of FE & 2 definitions of century
gen skratiohatz_area`i'_dearly`i'_dumkl=skratiohatz_area`i'_dearly`i'*dumkl
gen skratiohatz_area`i'_dlate`i'_dumkl=skratiohatz_area`i'_dlate`i'*dumkl
gen skratiohatz_noind`i'_dearly`i'_dumkl=skratiohatz_noind`i'_dearly`i'*dumkl
gen skratiohatz_noind`i'_dlate`i'_dumkl=skratiohatz_noind`i'_dlate`i'*dumkl
gen skratiohatz_indyr`i'_dearly`i'_dumkl=skratiohatz_indyr`i'_dearly`i'*dumkl
gen skratiohatz_indyr`i'_dlate`i'_dumkl=skratiohatz_indyr`i'_dlate`i'*dumkl

gen skratiohatz_area`i'_dearly`i'_dumky=skratiohatz_area`i'_dearly`i'*dumky
gen skratiohatz_area`i'_dlate`i'_dumky=skratiohatz_area`i'_dlate`i'*dumky
gen skratiohatz_noind`i'_dearly`i'_dumky=skratiohatz_noind`i'_dearly`i'*dumky
gen skratiohatz_noind`i'_dlate`i'_dumky=skratiohatz_noind`i'_dlate`i'*dumky
gen skratiohatz_indyr`i'_dearly`i'_dumky=skratiohatz_indyr`i'_dearly`i'*dumky
gen skratiohatz_indyr`i'_dlate`i'_dumky=skratiohatz_indyr`i'_dlate`i'*dumky

}

save "$temp/regdata_estruct_full.dta", replace


* Generate same data on immigrants
sort area statea
merge m:1 area statea using "$dta/temp_newimm.dta"
drop _merge
replace newimm1880=0 if newimm1880==.


gen nonewimm1880=(1-newimm1880)

forvalues i=1(1)2{
gen skhat_dearly`i'_newimm=skratiohat_dearly`i'*newimm1880
gen skhat_dlate`i'_newimm=skratiohat_dlate`i'*newimm1880
gen skhat_dearly`i'_nonewimm=skratiohat_dearly`i'*nonewimm1880
gen skhat_dlate`i'_nonewimm=skratiohat_dlate`i'*nonewimm1880
gen skhatz_noind`i'_dearly`i'_newimm = skratiohatz_noind`i'_dearly`i'*newimm1880
gen skhatz_noind`i'_dlate`i'_newimm = skratiohatz_noind`i'_dlate`i'*newimm1880
gen skhatz_noind`i'_dearly`i'_nonewimm = skratiohatz_noind`i'_dearly`i'*nonewimm1880
gen skhatz_noind`i'_dlate`i'_nonewimm = skratiohatz_noind`i'_dlate`i'*nonewimm1880


gen skhatz_area`i'_dearly`i'_newimm = skratiohatz_area`i'_dearly`i'*newimm1880
gen skhatz_area`i'_dlate`i'_newimm = skratiohatz_area`i'_dlate`i'*newimm1880
gen skhatz_area`i'_dearly`i'_nonewimm = skratiohatz_area`i'_dearly`i'*nonewimm1880
gen skhatz_area`i'_dlate`i'_nonewimm = skratiohatz_area`i'_dlate`i'*nonewimm1880

gen skhatz_indyr`i'_dearly`i'_newimm = skratiohatz_indyr`i'_dearly`i'*newimm1880
gen skhatz_indyr`i'_dlate`i'_newimm = skratiohatz_indyr`i'_dlate`i'*newimm1880
gen skhatz_indyr`i'_dearly`i'_nonewimm = skratiohatz_indyr`i'_dearly`i'*nonewimm1880
gen skhatz_indyr`i'_dlate`i'_nonewimm = skratiohatz_indyr`i'_dlate`i'*nonewimm1880

}

save "$temp/regdata_estruct_full.dta", replace


erase "$temp/regdata_k.dta"
erase "$temp/regdata_kl.dta"
erase "$temp/regdata_ky.dta"
